package com.kc.bus.service.impl;

import com.kc.bus.domain.Goods;
import com.kc.bus.domain.Inport;
import com.kc.bus.mapper.GoodsMapper;
import com.kc.bus.mapper.InportMapper;
import com.kc.bus.service.GoodsService;
import com.kc.bus.service.InportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 老杜
 * @since 2019-11-04
 *
 */

@Service
@Transactional
public class InportServiceImpl extends ServiceImpl<InportMapper, Inport> implements InportService {

    @Autowired
    private GoodsService goodsService;

    @Override
    public boolean save(Inport entity) {
        //根据商品编号查询商品
        Goods goods = goodsService.getById(entity.getGoodsid());
        //更新商品库存
        goods.setNumber(goods.getNumber()+entity.getNumber());
        goodsService.updateById(goods);
        //保存进货信息
        return super.save(entity);
    }

    @Override
    public boolean updateById(Inport entity) {
        //根据进货Id查询进货
        Inport inport = this.getBaseMapper().selectById(entity.getId());
        //根据商品Id查询商品信息
        Goods goods = this.goodsService.getById(entity.getGoodsid());
        //最后的库存算法=当前库存 - 进货单修改之前的数量 + 修改之后的数量
        goods.setNumber(goods.getNumber() - inport.getNumber() + entity.getNumber());
        //更新库存表数量
        goodsService.updateById(goods);
        //更新进货 单
        return super.updateById(entity);
    }
    //重写删除的方法
    @Override
    public boolean removeById(Serializable id) {
        //根据进货Id查询进货
        Inport inport = this.getBaseMapper().selectById(id);
        //根据商品Id查询商品信息
        Goods goods = this.goodsService.getById(inport.getGoodsid());
        //最后的库存算法=当前库存 - 进货单的数量
        goods.setNumber(goods.getNumber() - inport.getNumber());
        //更新库存表数量
        goodsService.updateById(goods);
        return super.removeById(id);
    }
}
